import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

class PushFirstApp extends StatelessWidget {
  const PushFirstApp({Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title:'Willian',
      debugShowCheckedModeBanner: false,
      debugShowMaterialGrid: false,
      theme: ThemeData(
        primaryColor: Colors.orange
      ),
      home: PushFirstPage(),
    );
  }
}

const pushMainPlateform = const MethodChannel('willian.flutter.io/willian_push_main');

class PushFirstPage extends StatefulWidget {
  PushFirstPage({Key key}) : super(key: key);

  @override
  _PushFirstPageState createState() => _PushFirstPageState();
}

class _PushFirstPageState extends State<PushFirstPage> {


  Future<dynamic> handelPushCall(MethodCall methcall){
    print(methcall.toString());

    String backResult = "Flutter go back success";
    if(methcall.method == "goback"){
      if(Navigator.of(this.context).canPop()){
        Navigator.of(this.context).pop();
      }else{
        backResult = "Flutter go back error";
      }
    }
    return Future.value(backResult);
  }

  void pushNativePage(){
    pushMainPlateform.invokeMapMethod('pushNativePage', {'key1':'value1'});
  }

  @override
  Widget build(BuildContext context) {

    pushMainPlateform.setMethodCallHandler(handelPushCall);

    return Scaffold(
      // push 过来的页面，原生没隐藏导航栏，所以把flutter页面的导航栏隐藏
//      appBar: new AppBar(
//        title: Text('First'),
//      ),
        body: Center (
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            crossAxisAlignment: CrossAxisAlignment.center,
            children: <Widget>[
              new Text('First Page Content'),
              new RaisedButton(
                onPressed: (){
                  Navigator.of(context).push(
                    MaterialPageRoute(
                      builder: (context) => PushSecondPage(),
                    ),
                  );
                },
                child: Text('Go to Flutter Second Page'),
              ),
              new RaisedButton(
                onPressed: (){
                  SystemNavigator.pop();
                },
                child: Text('Close Flutter Page'),
              ),
              new RaisedButton(
                onPressed: (){
                  pushNativePage();
                },
                child: Text('Go to Native Page'),
              ),
            ],
          ),
        )
    );
  }
}

class PushSecondPage extends StatelessWidget {

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      // 同上
//      appBar: new AppBar(
//        title: Text('SecondPage'),
//      ),
      body: new Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: <Widget>[
            new Text('Second Flutter Page Content'),
            new RaisedButton(
              onPressed: (){
                SystemNavigator.pop();
              },
              child: Text('Close All Flutter Page'),
            ),
            new RaisedButton(
              onPressed: (){
                pushMainPlateform.invokeMethod('pushNativePage', {'key1':'value1'});
              },
              child: Text('Go To Native Page'),
            ),
          ],
        ),
      ),
    );
  }
}
